<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>后台管理系统</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Bootstrap 3.3.6 -->
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
    <!-- AdminLTE Skins. We have chosen the skin-blue for this starter
    page. However, you can choose any other skin. Make sure you
    apply the skin class to the body tag so the changes take effect.
    -->
    <link rel="stylesheet" href="dist/css/skins/skin-blue.min.css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
<#include "header.ftl" />
<#include "sidebar.ftl" />
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper" id="app">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                系统管理
                <small>角色管理</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> 系统管理</a></li>
                <li class="active">角色管理</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">
            <div class="box">
                <div class="box-header">
                <@shiro.hasPermission name="system:sys_role:add">
                    <button type="button" class="btn btn-info" @click="addRole()"><i class="fa fa-user-plus"></i> 新增角色
                    </button>
                </@shiro.hasPermission>
                </div>
                <div class="box-body table-responsive no-padding">
                    <table class="table table-hover">
                        <tr>
                            <th>ID</th>
                            <th>角色名</th>
                            <th>描述</th>
                            <th>操作</th>
                        </tr>
                        <tr v-for="role in roles">
                            <td>{{ role.id }}</td>
                            <td>{{ role.name }}</td>
                            <td>{{ role.description }}</td>
                            <td>
                            <@shiro.hasPermission name="system:sys_role:modify">
                                <button type="button" class="btn btn-primary"
                                        @click="modifyPermissions(role)">
                                    权限分配
                                </button>
                            </@shiro.hasPermission>
                            <@shiro.hasPermission name="system:sys_role:del">
                                <button type="button" class="btn btn-danger"
                                        @click="delRole(role)">
                                    删除角色
                                </button>
                            </@shiro.hasPermission>
                            </td>
                        </tr>
                    </table>
                    <modal :show.sync="showModal" title="权限分配" effect="fade">
                        <div slot="modal-body" class="modal-body">
                            <table class="table table-hover">
                                <tr>
                                    <th>名称</th>
                                </tr>
                                <tr v-for="permission in permissions">
                                    <td>
                                        <input type="checkbox" v-model="permissionIds" value="{{ permission.id }}"
                                               v-checked="isChecked(permission.id)">
                                        {{ permission.name }}
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <div slot="modal-footer" class="modal-footer">
                            <button type="button" class="btn btn-default" @click='closeModal()'>关闭</button>
                            <button type="button" class="btn btn-primary" @click='saveRolePermissions()'>保存</button>
                        </div>
                    </modal>
                <@shiro.hasPermission name="system:sys_role:add">
                    <modal :show.sync="showRoleModal" title="新增角色" effect="fade">
                        <div slot="modal-body" class="modal-body">
                            <form class="form-horizontal">
                                <div class="form-group">
                                    <label for="inputRoleName" class="col-sm-2 control-label">角色名</label>

                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="inputRoleName" v-model="role.name"
                                               placeholder="角色名">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="inputDescription" class="col-sm-2 control-label">角色描述</label>

                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="inputDescription"
                                               v-model="role.description" placeholder="角色描述">
                                    </div>
                                </div>
                            </form>
                        </div>
                        <div slot="modal-footer" class="modal-footer">
                            <button type="button" class="btn btn-default" @click='closeRoleModal()'>关闭</button>
                            <button type="button" class="btn btn-primary" @click='doAddRole()'>保存</button>
                        </div>
                    </modal>
                </@shiro.hasPermission>
                </div>
                <!-- /.box-body -->
                <div class="box-footer clearfix">
                    <ul class="pagination pagination-sm no-margin pull-right">
                        <li class="first" v-bind:class="{'disabled': first}">
                            <a v-if="first">首页</a>
                            <a v-else @click="toFirst()">首页</a>
                        </li>
                        <li class="prev" v-bind:class="{'disabled': first}">
                            <a v-if="first">上一页</a>
                            <a v-else @click="toPrev()">上一页</a>
                        </li>
                        <template v-for="index in totalPages">
                            <li class="paginate_button {{classRenderer(index)}}">
                                <a @click="jumpTo(index)">{{ index + 1 }}</a>
                            </li>
                        </template>
                        <li class="next" v-bind:class="{'disabled': last}">
                            <a v-if="last">下一页</i></a>
                            <a v-else @click="toNext()">下一页</i></a>
                        </li>
                        <li class="last" v-bind:class="{'disabled': last}">
                            <a v-if="last">末页</a>
                            <a v-else @click="toLast()">末页</a>
                        </li>
                    </ul>
                </div>
            </div>
            <!-- /.box -->
        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->

<#include "footer.ftl" />

    <!-- Control Sidebar -->
    <aside class="control-sidebar control-sidebar-dark">
        <!-- Create the tabs -->
        <ul class="nav nav-tabs nav-justified control-sidebar-tabs">
            <li class="active"><a href="#control-sidebar-home-tab" data-toggle="tab"><i class="fa fa-home"></i></a></li>
            <li><a href="#control-sidebar-settings-tab" data-toggle="tab"><i class="fa fa-gears"></i></a></li>
        </ul>
        <!-- Tab panes -->
        <div class="tab-content">
            <!-- Home tab content -->
            <div class="tab-pane active" id="control-sidebar-home-tab">
                <h3 class="control-sidebar-heading">Recent Activity</h3>
                <ul class="control-sidebar-menu">
                    <li>
                        <a href="javascript::;">
                            <i class="menu-icon fa fa-birthday-cake bg-red"></i>

                            <div class="menu-info">
                                <h4 class="control-sidebar-subheading">Langdon's Birthday</h4>

                                <p>Will be 23 on April 24th</p>
                            </div>
                        </a>
                    </li>
                </ul>
                <!-- /.control-sidebar-menu -->

                <h3 class="control-sidebar-heading">Tasks Progress</h3>
                <ul class="control-sidebar-menu">
                    <li>
                        <a href="javascript::;">
                            <h4 class="control-sidebar-subheading">
                                Custom Template Design
                                <span class="label label-danger pull-right">70%</span>
                            </h4>

                            <div class="progress progress-xxs">
                                <div class="progress-bar progress-bar-danger" style="width: 70%"></div>
                            </div>
                        </a>
                    </li>
                </ul>
                <!-- /.control-sidebar-menu -->

            </div>
            <!-- /.tab-pane -->
            <!-- Stats tab content -->
            <div class="tab-pane" id="control-sidebar-stats-tab">Stats Tab Content</div>
            <!-- /.tab-pane -->
            <!-- Settings tab content -->
            <div class="tab-pane" id="control-sidebar-settings-tab">
                <form method="post">
                    <h3 class="control-sidebar-heading">General Settings</h3>

                    <div class="form-group">
                        <label class="control-sidebar-subheading">
                            Report panel usage
                            <input type="checkbox" class="pull-right" checked>
                        </label>

                        <p>
                            Some information about this general settings option
                        </p>
                    </div>
                    <!-- /.form-group -->
                </form>
            </div>
            <!-- /.tab-pane -->
        </div>
    </aside>
    <!-- /.control-sidebar -->
    <!-- Add the sidebar's background. This div must be placed
         immediately after the control sidebar -->
    <div class="control-sidebar-bg"></div>
</div>
<!-- ./wrapper -->

<!-- REQUIRED JS SCRIPTS -->

<!-- jQuery 2.2.0 -->
<script src="plugins/jQuery/jQuery-2.2.0.min.js"></script>
<!-- Bootstrap 3.3.6 -->
<script src="bootstrap/js/bootstrap.min.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/app.min.js"></script>
<#--Vue.js-->
<script src="vue/vue.min.js"></script>
<script src="vue/vue-resource.min.js"></script>
<script src="vue/vue-strap.min.js"></script>
<script>
    var modal = VueStrap.modal;
    // add custom directive
    Vue.directive('checked', function (val) {
        this.el.checked = val;

        if ("createEvent" in document) {
            var evt = document.createEvent("HTMLEvents");
            evt.initEvent("change", false, true);
            this.el.dispatchEvent(evt);
        }
        else {
            this.el.fireEvent("onchange");
        }
    });
    new Vue({
        el: '#app',
        data: {
            roles: [],
            showModal: false,
            showRoleModal: false,
            permissions: [],
            role_permissions: [],
            permissionIds: [],
            role: {},
            currentRole: {},
            totalPages: "",
            totalElements: "",
            last: "",
            size: "",
            number: 0,
            sort: [],
            first: "",
            numberOfElements: ""
        },
        components: {
            modal
        },
        ready: function () {
            this.$http.get("/roles/list", function (data) {
                this.roles = data.content;
                this.first = data.first;
                this.last = data.last;
                this.totalPages = data.totalPages;
                this.number = data.number;
                this.getPermissionList();
            })
        },
        methods: {
            classRenderer: function (index) {
                if (index == this.number) {
                    return 'active';
                }
                return '';
            },
            toFirst: function () {
                this.getList(0);
            },
            toLast: function () {
                this.getList(this.totalPages - 1);
            },
            toPrev: function () {
                this.getList(this.number - 1);
            },
            toNext: function () {
                this.getList(this.number + 1);
            },
            jumpTo: function (index) {
                this.getList(index);
            },
            getList: function (page) {
                this.$http.get("/roles/list", {page: page}).then(function (response) {
                    var data = response.data;
                    this.roles = data.content;
                    this.first = data.first;
                    this.last = data.last;
                    this.totalPages = data.totalPages;
                    this.number = data.number;
                }, function (data) {
                    console.log(data);
                })
            },
            getPermissionList: function () {
                this.$http.get("/permissions/list").then(function (response) {
                    this.permissions = response.data;
                }, function (data) {
                    console.log(data);
                })
            },
            isChecked: function (id) {
                for (var i = 0; i < this.role_permissions.length; i++) {
                    if (id == this.role_permissions[i].id) {
                        return true;
                    }
                }
                return false;
            },
            saveRolePermissions: function () {
                var resource = this.$resource('/roles');
                var rolePermission = {
                    "name": this.currentRole.name,
                    "permissionIds": this.permissionIds
                };
                resource.update(rolePermission).then(function (response) {
                    this.getList(0);
                }, function (response) {
                    console.log(response);
                })
                this.showModal = false;
            },
            modifyPermissions: function (currentRole) {
                this.currentRole = currentRole;
                this.role_permissions = currentRole.permissions;
                this.showModal = true;
            },
            closeModal: function () {
                this.showModal = false;
            },
            addRole: function () {
                this.role = {};
                this.showRoleModal = true;
            },
            doAddRole: function () {
                var role = {
                    name: this.role.name,
                    description: this.role.description
                }
                this.$http.post("/roles", role).then(function (data) {
                    this.getList(this.number);
                    this.showRoleModal = false;
                })
            },
            delRole: function(role) {
                this.$http.delete("/roles", role).then(function(data) {
                    this.getList(0);
                })
            },
            closeRoleModal: function () {
                this.showRoleModal = false;
            }
        }
    })
</script>

</body>
</html>